﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Library
{
    class TruncatedSplitting: ISplitting
    {
        public IList<string> SplitandTruncateStrings(string wordList)
        {
            IList<string> words = wordList.Split('\n', '\r').ToList();
            var wordCount = words.Count;
            for(int i = 0; i<wordCount; i++)
            {
                if (string.IsNullOrWhiteSpace(words[i]))
                {
                    words.RemoveAt(i--);
                    wordCount--;
                }
            }

            return words;
        }

        public IEnumerable<string> Split(string wordList) 
        {
            IList<string> words = wordList.Split(' ');
            var wordCount = words.Count;
            for (int i = 0; i < wordCount; i++)
            {
                yield return words[i].ToLower(CultureInfo.InvariantCulture);
            }

        }

        public IList<string> WhiteSpaceBasedSplitting(string line)
        {
            IList<string> words = new List<string>();
            int length = line.Length;
            string word = string.Empty;
            for (int i = 0; i < length; i++)
            {
                if ((char.IsWhiteSpace(line[i])))
                {
                    if(!string.IsNullOrWhiteSpace(word))
                    {
                        words.Add(word);
                        word = string.Empty;
                    }
                    else
                    {
                        word = string.Empty;
                    }
                }
                else
                {
                    word += line[i];
                }
            }

            return words;
        }
    }
}
